package dao.impl;

import Utils.db.dao.BaseDAO;
import dao.TaskDAO;
import pojo.Task;

import java.sql.SQLException;
import java.util.List;

/**
 * @author d11yu
 */
public class TaskDAOImpl extends BaseDAO implements TaskDAO {

    @Override
    public Integer addTask(Task task) {
        String sql = "insert into task (taskname, status, time_update, time_create, user_id) values (?, ?, ?, ?, ?)";
        return update(sql, task.getTaskname(), task.getStatus(), task.getTimeUpdate(), task.getTimeCreate(), task.getUserId());
    }

    @Override
    public Integer deleteTaskById(Integer id) {
        String sql = "delete from task where id= ?";
        return update(sql, id);
    }

    @Override
    public Integer updateTaskById(Task task) {
        String sql = "update task set taskname = ?, status = ?, time_update = ?, time_create = ?, user_id = ? where id = ?";
        return update(sql, task.getTaskname(), task.getStatus(), task.getTimeUpdate(), task.getTimeCreate(), task.getUserId(), task.getId());
    }

    @Override
    public List<Task> queryAllTasks() throws SQLException {
        String sql = "select id, taskname, status, time_update as timeUpdate, time_create as timeCreate, user_id as userId from task";
        return queryBeanList(Task.class, sql);
    }

    @Override
    public List<Task> queryAllTasksByUserId(Integer userId) throws SQLException {
        String sql = "select id, taskname, status, time_update as timeUpdate, time_create as timeCreate, user_id as userId from task where user_id = ?";
        return queryBeanList(Task.class, sql, userId);
    }

    @Override
    public Task queryTaskById(Integer id) throws SQLException {
        String sql = "select id, taskname, status, time_update as timeUpdate, time_create as timeCreate, user_id as userId from task where id = ?";
        return queryBean(Task.class, sql, id);
    }

    @Override
    public List<Task> queryTasksByTasknameBlur(String taskname) throws SQLException {
        String sql = "select id, taskname, status, time_update as timeUpdate, time_create as timeCreate, user_id as userId from task where taskname like ? order by timeUpdate desc";
        return queryBeanList(Task.class, sql, "%" + taskname + "%");
    }

    @Override
    public Integer getEct() throws SQLException {
        String sql = "select count(*) from task";
        Object o = queryScalar(sql);
        return ((Number) o).intValue();
    }

    /**
     * @return
     * @throws SQLException
     */
    @Override
    public Integer getEctTasknameBlur(String taskname) throws SQLException {
        String sql = "select count(*) from task where taskname like ?";
        return ((Number) queryScalar(sql, "%" + taskname + "%")).intValue();
    }

    @Override
    public Integer getEctTasknameBlurByUserId(String taskname, Integer userId) throws SQLException {
        String sql = "select count(*) from task where taskname like ? and user_id = ?";
        return ((Number) queryScalar(sql, "%" + taskname + "%", userId)).intValue();
    }

    @Override
    public Integer getEctByUserId(Integer userId) throws SQLException {
        String sql = "select count(*) from task where user_id = ?";
        return ((Number) queryScalar(sql, userId)).intValue();
    }

    @Override
    public List<Task> queryBeanListPerPage(Integer eStart, Integer ecp) throws SQLException {
        String sql = "select id, taskname, status, time_update as timeUpdate, time_create as timeCreate, user_id as userId from task order by time_update desc limit ?, ?";
        return queryBeanList(Task.class, sql, eStart, ecp);
    }

    /**
     * @param eStart
     * @param ecp
     * @return
     * @throws SQLException
     */
    @Override
    public List<Task> queryBeanListPerPageTasknameBlur(Integer eStart, Integer ecp, String taskname) throws SQLException {
        String sql = "select id, taskname, status, time_update as timeUpdate, time_create as timeCreate, user_id as userId from task where taskname like ? order by time_update desc limit ?, ?";
        return queryBeanList(Task.class, sql, "%" + taskname + "%", eStart, ecp);
    }

    @Override
    public List<Task> queryBeanListPerPageTasknameBlurByUserId(Integer eStart, Integer ecp, String taskname, Integer userId) throws SQLException {
        String sql = "select id, taskname, status, time_update as timeUpdate, time_create as timeCreate, user_id as userId from task where user_id = ? and taskname like ? order by time_update desc limit ?, ?";
        return queryBeanList(Task.class, sql, userId, "%" + taskname + "%", eStart, ecp);
    }

    @Override
    public List<Task> queryBeanListPerPageByUserId(Integer eStart, Integer ecp, Integer userId) throws SQLException {
        String sql = "select id, taskname, status, time_update as timeUpdate, time_create as timeCreate, user_id as userId from task where user_id = ? order by time_update desc limit ?, ?";
        return queryBeanList(Task.class, sql, userId, eStart, ecp);
    }
}
